package Bus;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import Main.Util;

/**
 * Fetch the train's information with bus's name, and save it into databaase
 * 
 * ����г��ĳ��Σ�������ץȡ���г�����Ϣ�����������ֶ�
 * 
 * bus_name					����
 * starting_station			ʼ��վ
 * ending_station			�յ�վ
 * total_station				��վ����
 * starting_bus_time		ʼ��վ��ĩ�೵ʱ��
 * ending_bus_time			�յ�վ��ĩ�೵ʱ��
 * bus_card_price			ˢ���˳��ļ۸�2λС��
 * fare							Ʊ�ƣ��ֶμƼ�or��һƱ��
 * price							ȫ��Ʊ�ۣ�2λС��
 * mileage						��̣�2λС��
 * 
 * ����Ϊһ����¼�����浽��ݿ�� beijing_bus_line����
 * 
 * @author buju.hh
 *
 */
public class BusLine {
	
	/**
	 * 
	<pid="info"class="description">
            起点站首末车时间:05:00-23:00<br>
	终点站首末车时间:05:00-23:00<br>
	刷卡:刷卡封顶成人4折/学生2折<br>
	票制:单一票制<br>
	全程票价(元):1.00;线路长度(公里):20.351<br></p>  
    
	 */
	
	private String bus_info;
	private String bus_name;
	private String starting_station;
	private String ending_station;
	private String total_station;
	private String starting_bus_time;
	private String ending_bus_time;
	private String bus_card_price;
	private String fare;
	private String price;
	private String mileage;
	
	private String NA = "NULL";
	private boolean DEBUG = true;
	
	public BusLine(String busName, String busInfo) {
		
		this.bus_info = busInfo;
		this.bus_name = busName;
		
		if((DEBUG)){
			System.out.println("\n-------- DEBUG BusLine START--------");
		}
		
		get_starting_station();
		get_ending_station();
		get_total_station();
		get_starting_bus_time();
		get_ending_bus_time();
		get_bus_card_price();
		get_fare();
		get_price();
		get_mileage();
		
		this.writeToDatabase();
		
		if(DEBUG) System.out.println("-------- DEBUG BusLine END--------\n\n");
	}
	
	private void get_starting_station(){
		Pattern pattern = Pattern.compile("--></div><h2>.*的途经公交站点"); 
		Matcher matcher = pattern.matcher(this.bus_info);

		if(matcher.find()){
			String temp = matcher.group();
			this.starting_station = temp.substring(temp.indexOf("(")+1, temp.indexOf("→"));
			if(DEBUG) System.out.println("starting_station: " + this.starting_station);
		}else{
			this.starting_station = this.NA;
			if(DEBUG) System.err.println("starting_station: FAIL");
		}
	}
	
	private void get_ending_station(){
		Pattern pattern = Pattern.compile("--></div><h2>.*的途经公交站点"); 
		Matcher matcher = pattern.matcher(this.bus_info);

		if(matcher.find()){
			String temp = matcher.group();
			this.ending_station = temp.substring(temp.indexOf("→")+1, temp.lastIndexOf(")"));
			if(DEBUG) System.out.println("ending_station: " + this.ending_station);
		}else{
			this.ending_station = this.NA;
			if(DEBUG) System.err.println("ending_station: FAIL");
		}
	}
	
	private void get_total_station(){		
		Pattern pattern = Pattern.compile("\\u005B[0-9]*站]"); 
		Matcher matcher = pattern.matcher(this.bus_info);

		if(matcher.find()){
			String temp = matcher.group();
			this.total_station = temp.substring(1, temp.indexOf("站"));
			if(DEBUG) System.out.println("total_station: " + this.total_station);
		}else{
			this.total_station = this.NA;
			if(DEBUG) System.err.println("total_station: FAIL");
		}
	}
	
	private void get_starting_bus_time(){
		Pattern pattern = Pattern.compile("起点站首末车时间:.*?<br>"); 
		Matcher matcher = pattern.matcher(this.bus_info);

		if(matcher.find()){
			String temp = matcher.group();
			
			if(temp.indexOf(":终") > -1){
				this.starting_bus_time = temp.substring(9, temp.indexOf(":终"));
			}else if(temp.indexOf("终点") > -1){
				this.starting_bus_time = temp.substring(9, temp.indexOf("终点"));
			}else{
				this.starting_bus_time = temp.substring(9, temp.lastIndexOf("<br>"));
			}			
			
			if(DEBUG) System.out.println("starting_bus_time:" + this.starting_bus_time);
		}else{
			this.starting_bus_time = this.NA;
			if(DEBUG) System.err.println("starting_bus_time: FAIL");
		}
	}
	
	private void get_ending_bus_time(){
		Pattern pattern = Pattern.compile("终点站首末车时间:.*?<br>"); 
		Matcher matcher = pattern.matcher(this.bus_info);

		if(matcher.find()){
			String temp = matcher.group();
			this.ending_bus_time = temp.substring(9, temp.lastIndexOf("<br>"));
			if(DEBUG) System.out.println("ending_bus_time:" + this.ending_bus_time);
		}else{
			this.ending_bus_time = this.NA;
			if(DEBUG) System.err.println("ending_bus_time: FAIL");
		}
	}
	
	private void get_bus_card_price(){
		Pattern pattern = Pattern.compile("刷卡:.*?<br>"); 
		Matcher matcher = pattern.matcher(this.bus_info);

		if(matcher.find()){
			String temp = matcher.group();
			this.bus_card_price = temp.substring(3, temp.lastIndexOf("<br>"));
			if(DEBUG) System.out.println("bus_card_price:" + this.bus_card_price);
		}else{
			this.bus_card_price = this.NA;
			if(DEBUG) System.err.println("bus_card_price: FAIL");
		}
	}
	
	private void get_fare(){
		Pattern pattern = Pattern.compile("票制:.*?<br>"); 
		Matcher matcher = pattern.matcher(this.bus_info);

		if(matcher.find()){
			String temp = matcher.group();
			
			if(temp.indexOf("全程") > -1){
				this.fare = this.NA;
			}else{
				this.fare = temp.substring(3, temp.lastIndexOf("<br>"));
			}
						
			if(DEBUG) System.out.println("fare:" + this.fare);
		}else{
			this.fare = this.NA;
			if(DEBUG) System.err.println("fare: FAIL");
		}
	}
	
	private void get_price(){
		Pattern pattern = Pattern.compile("全程票价.*线路长度"); 
		Matcher matcher = pattern.matcher(this.bus_info);

		if(matcher.find()){
			String temp = matcher.group();
			if(temp.indexOf(";")>-1){
				this.price = temp.substring(8, temp.indexOf(";"));
			}else if(temp.indexOf("<br>")>-1){
				this.price = temp.substring(8, temp.indexOf("<br>"));
			}			
			if(DEBUG) System.out.println("price:" + this.price);
		}else{
			this.price = this.NA;
			if(DEBUG) System.err.println("price: FAIL");
		}
	}
	
	private void get_mileage(){
		Pattern pattern = Pattern.compile("线路长度.*<br>"); 
		Matcher matcher = pattern.matcher(this.bus_info);

		if(matcher.find()){
			String temp = matcher.group();
			this.mileage = temp.substring(9, temp.indexOf("<")).replace("公里", ""); //有的数据带"公里"，去掉
			if(DEBUG) System.out.println("mileage:" + this.mileage);
		}else{
			this.mileage = this.NA;
			if(DEBUG) System.err.println("mileage: FAIL");
		}
	}
	
	
	private void writeToDatabase(){
		ArrayList<String> data = new ArrayList<String>();
		data.add(bus_name);
		data.add(starting_station);
		data.add(ending_station);
		data.add(total_station);
		data.add(starting_bus_time);
		data.add(ending_bus_time);
		data.add(bus_card_price);
		data.add(fare);
		data.add(price);
		data.add(mileage);
		
		int ret = Util.writeToLocalDB_bus_line(data);
		
		if(ret == 1){
			System.out.println(Util.getCurrentTime() + "	Write database [" + bus_name + "] - Done");
		}else{
			System.out.println(Util.getCurrentTime() + "	Write database [" + bus_name + "] - Fail");
		}
	}
	
	private String formatTime(String time){
		String hour, min;
		if( time.indexOf("Сʱ") > -1){
			hour = time.substring(0, time.indexOf("Сʱ"));
			min = time.substring(time.indexOf("ʱ") + 1, time.length() - 1);
		}else{
			hour = "00";
			min = time.substring(0, time.length() - 1);
		}
		
		hour = (hour.length() < 2) ? ("0" + hour) : hour;
		min = (min.length() < 2) ? ("0" + min) : min;
		
		return hour + ":" + min + ":" + "00";
	}
	
	private String formatPrice(String time){
		String afterFormat = "";		
		
		if(time.equals("0")){
			afterFormat = time;
		}else if(!time.equals("-")){
			if(time.indexOf(".") > -1){
				String[] a = time.split("\\.");
				afterFormat = a[0] + a[1];
			}else if(time.equals("")){
				afterFormat = NA;
			}else{
				afterFormat = time + "0";
			}
			
		}else
			afterFormat = NA;
		
		return afterFormat;
	}
	
}
